/*
* 单选组件
* */

import 'package:center_beijing/common/styles.dart';
import 'package:center_beijing/models/selectItem.dart';
import 'package:flutter/material.dart';

class CustomRadio extends StatefulWidget {
  final String title;
  final List<SelectItems> selectList;
  final Function selectCallback;
  final bool ifRequired;
  final double top;
  final SelectItems currentItem;
  CustomRadio(
      {this.title='',@required this.selectList,this.selectCallback,this.ifRequired=false,this.top=Styles.spacing8,this.currentItem});

  @override
  _CustomRadioState createState() => _CustomRadioState();
}

class _CustomRadioState extends State<CustomRadio> with SingleTickerProviderStateMixin {
  AnimationController _controller;
  SelectItems _newValue = SelectItems();
  @override
  void initState() {
    _controller = AnimationController(vsync: this);
    super.initState();
    this.setState(() {
      _newValue = widget.currentItem??widget.selectList[0];
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top:  widget.top),
      padding: EdgeInsets.fromLTRB(
          Styles.spacing16, Styles.spacing8, Styles.spacing16, Styles.spacing8),
      color: Colors.white,
      child:Row(
        children: [
          widget.title!=''?Expanded(flex:1,child: Row(
            children: [
              widget.ifRequired
                  ? Text(
                "*",
                style: TextStyle(
                    fontSize: Styles.NormalFont,
                    color:
                    Color.fromRGBO(242, 114, 94, 1)),
              )
                  : Container(),
              Container(
                child: Text(widget.title,style: TextStyle(
                    color: Styles.LeftTitleColor,fontSize: Styles.NormalFont
                ),),
              )
            ],
          ),):Container(),
          Expanded(flex:2,child: Row(
            children: widget.selectList.asMap().keys.map((index){
              return Expanded(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        widget.selectList[index].label,
                        style: TextStyle(
                            color: Styles.DetailColor,
                            fontSize: Styles.NormalFont),
                      ),
                      Radio<String>(
                          value: widget.selectList[index].label,
                          activeColor: Styles.BlueColor,
                          groupValue: _newValue.label,
                          onChanged: (value) {
                            widget.selectList.forEach((element) {
                              if(element.label==value){
                                setState(() {
                                  _newValue = element;
                                });
                                widget.selectCallback(element);
                              }
                            });
                          }),
                    ],
                  ));
            }).toList(),
          ))
        ],
      ),
    );
  }
}
